
require(['jquery','Bswiper'],function($,aswiper){
    //图片滑动
    var aswiper = new Swiper('.swiper-container', {
        pagination:'.swiper-pagination',
        paginationClickable: true
    });
})

//设置html根目录字体大小
require(['jquery','../modules/pageSize'],function($,pageSetSize){
    setRootFontSize(pageSetSize);
});

//闪购菜单
require(['jquery','../modules/popup'],function($,popup){


    var isEnd = false, isAjax = false;
    var special_id = '';
    var $tid= '';
    var $sort = 1;
    var _state ='';
    //闪购time
    var parT = $('.rush-date').width();
    $('.rush-date li').css('width',parT/4);
    var li_width = $('.rush-date li').width();
    var li_len =$('.rush-date li').length;
    var ul_width = li_width*li_len;
    $('.rush-date ul').css('width',ul_width);
    $('.rush-date li').on('click',ajaxRushDate);
    $('.rush-date li').each(function(){
        if($(this).hasClass('active')){
            special_id =  $('.rush-date li').attr('data-special-id');
        }
    });
    var realInventory = '',outInventory = '';
    goodsListGet(special_id);
    /**
     *闪购动态渲染页面
     * @ ajaxRushDate
     * @ ajaxRushMenu
     */
    function ajaxRushDate(){
        var _self = $(this);
        special_id = _self.attr('data-special-id');
        _self.addClass('active').siblings().removeClass('active');
        isAjax =  false;
        goodsListGet(special_id);
    }
    function ajaxRushMenu(){
        var _self = $(this);
        _state = _self.find('span').text();
        $tid = _self.attr('data-id');
        _self.addClass('active').siblings().removeClass('active');
        $sort =1;
        isAjax =  false;
        goodsListGetRes($tid,$sort,_state);
    }
    function goodsListGet(special_id){

        if(isEnd == true || isAjax == true){
            return false;
        }
        var $time =Date.parse(new Date())/1000;
        var html = '';
        var timeHtml = '';
        var _rushBannerHtml = '';
        var url = '/Home/Flashsales/timeProduct'+getFixedExtraUrl();
        var data ={special_id:special_id};
        isAjax =  true;
        $.ajax({
            url:url,
            type:"get",
            data:data,
            dataType:'json',
            success:function(data){
                isAjax =  false;
                var rushMes = data.list;
                var rushImg= rushMes.image;
                if(rushMes){
                    var _goodsList = rushMes.product_data;
                    var _rushTime = rushMes.time_data;
                    var _rushBanner = rushMes.starbuyImages;
                }
                else{return false;}
                if(_rushBanner){
                    for(var k=0; k<_rushBanner.length; k++){
                        _rushBannerHtml += '<div class="swiper-slide"><img src="'+_rushBanner[k].url+'"></div>';
                    }
                }

                if(_rushTime){
                    for(var j = 0; j<_rushTime.length; j++){
                        if($time >= _rushTime[j].begin_time && $time < _rushTime[j].end_time){
                            timeHtml +='<li class="active" style="width: 93.75px;" data-id="'+_rushTime[j].tid+'">';
                            timeHtml +='<a href="javascript:void(0);"><p>'+_rushTime[j].b_h_minute+'</p><span>抢购进行中</span></a>';
                            timeHtml +='</li>';
                        }else if($time< _rushTime[j].begin_time){
                            timeHtml +='<li style="width: 93.75px;" data-id="'+_rushTime[j].tid+'">';
                            timeHtml +='<a href="javascript:void(0);"><p>'+_rushTime[j].b_h_minute+'</p><span>即将开抢</span></a>';
                            timeHtml +='</li>';
                        }else if($time> _rushTime[j].end_time){
                            timeHtml +='<li style="width: 93.75px;" data-id="'+_rushTime[j].tid+'">';
                            timeHtml +='<a href="javascript:void(0);"><p>'+_rushTime[j].b_h_minute+'</p><span>已结束</span></a>';
                            timeHtml +='</li>';
                        }
                    }
                }
                if(_goodsList){
                    for(var i = 0; i<_goodsList.length; i++){
                        var rushCurrentImg ='' ;
                        realInventory = _goodsList[i].ini_y_tnum;                   
                        outInventory = _goodsList[i].ini_y_tnum - _goodsList[i].initnum;
                        //console.log(realInventory,outInventory);
                        var rushGoodId = _goodsList[i].goods_id;
                        if(rushImg){
                            rushCurrentImg = rushImg[rushGoodId];
                        }
                        html +='<div class="goods-item clearfix">';
                        html +='<a href="' +_goodsList[i].url + '" class="img"><img src="'+getDefaultImg(rushCurrentImg,true)+'" alt=""></a>';
                        html +='<div class="text">';
                        html +='<a href="' +_goodsList[i].url + '" class="goods-name">'+_goodsList[i].p_name+'</a>';
                        html +='<p class="clearfix"><span class="sale-price">￥'+_goodsList[i].promotion_price+'</span><span class="old-price">￥'+_goodsList[i].price+'</span></p>';
                        html +='<p>闪购库存：'+realInventory+'</p>';
                        html +='<p>已抢购：'+outInventory+'</p>';
                        if($time< _rushTime[_rushTime.length-1].begin_time){
                            html += '<p><a href="' +_goodsList[i].url + '" class="rushing-buy">去看看</a></p>';
                        }
                        else if($time >= _rushTime[_rushTime.length-1].begin_time && $time < _rushTime[_rushTime.length-1].end_time){
                            if (realInventory == outInventory) {
                                html += '<p><a href="' +_goodsList[i].url + '" class="rushing-buy"c style="background: #787878">已抢光</a></p>';
                            }
                            else{
                                html += '<p><a href="' +_goodsList[i].url + '" class="rushing-buy">马上抢</a></p>';
                            }
                        
                        }
                        else if($time > _rushTime[_rushTime.length-1].end_time){
                            html += '<p><a href="' +_goodsList[i].url + '" class="rushing-buy" style="background: #787878">已结束</a></p>';
                        }
                        html +='</div>';
                        html +='</div>';
                    }
                }
                $('.swiper-wrapper').html(_rushBannerHtml);
                $('.goods-horizontal').html(html);
                $('.rush-menu ul').html(timeHtml);
                rushMenuW();
            },
            error:function(){
                popup.alertPopup("服务器异常,请稍后重试！");
            }
        })
    }
    function goodsListGetRes($tid,$sort,_state){
        console.log(_state);
        if(isEnd == true || isAjax == true){
            return false;
        }
        var html = '';
        var url = '/Home/Flashsales/flashsalesProducts'+getFixedExtraUrl();
        var data ={tid:$tid,type:$sort};
        isAjax =  true;
        $.ajax({
            url:url,
            type:"get",
            data:data,
            dataType:'json',
            success:function(data){
                //console.log(data);
                isAjax =  false;
                var _goodsList = data.list;
                var rushImg= data.image;
                if (_goodsList) {
                    for (var i = 0; i < _goodsList.length; i++) {
                        var rushCurrentImg = '';
                        var rushGoodId = _goodsList[i].goods_id;
                        realInventory = _goodsList[i].ini_y_tnum;                   
                        outInventory = _goodsList[i].ini_y_tnum - _goodsList[i].initnum;
                        //console.log(realInventory,outInventory);
                        if (rushImg) {
                            rushCurrentImg = rushImg[rushGoodId];
                        }
                        html += '<div class="goods-item clearfix">';
                        html += '<a  href="' +_goodsList[i].url + '" class="img"><img src="'+getDefaultImg(rushCurrentImg,true)+'" alt=""></a>';
                        html += '<div class="text">';
                        html += '<a href="' +_goodsList[i].url + '" class="goods-name">' + _goodsList[i].p_name + '</a>';
                        html += '<p class="clearfix"><span class="sale-price">￥' + _goodsList[i].promotion_price + '</span><span class="old-price">￥' + _goodsList[i].price + '</span></p>';
                        html +='<p>闪购库存：'+realInventory+'</p>';
                        html +='<p>已抢购：'+outInventory+'</p>';
                        if (_state == '已结束') {
                            html += '<p><a href="' +_goodsList[i].url + '" class="rushing-buy" style="background: #787878">已结束</a></p>';
                        }
                        if (_state == '即将开抢') {
                            html += '<p><a href="' +_goodsList[i].url + '" class="rushing-buy">去看看</a></p>';
                        }
                        if (_state == '抢购进行中') {

                            if (realInventory == outInventory) {
                                html += '<p><a href="' +_goodsList[i].url + '" class="rushing-buy"c style="background: #787878">已抢光</a></p>';
                            }
                            else{
                                html += '<p><a href="' +_goodsList[i].url + '" class="rushing-buy">马上抢</a></p>';
                            }
                        }
                        html += '</div>';
                        html += '</div>';
                    }
                }
                $('.goods-horizontal').html(html);
            },
            error:function(){
                popup.alertPopup("服务器异常,请稍后重试！");
            }
        })
    }

    //闪购菜单
    function rushMenuW(){
        var parW = $('.rush-menu').width();
        $('.rush-menu li').css('width',parW/4);
        var _li_width = $('.rush-menu li').width();
        var _li_len =$('.rush-menu li').length;
        var _ul_width = _li_width*_li_len;
        $('.rush-menu ul').css('width',_ul_width);
        $('.rush-menu').on('click','li',ajaxRushMenu);
    }
})
